package com.lwd.controller;

import com.lwd.entity.User;
import com.lwd.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("doRegisterLogin")
    public String doRegisterLogin(User user,Model model) {

        boolean flag = userService.register(user);

        if (flag) {
            model.addAttribute("message","恭喜你注册成功，请登录");
            return "user-login";
        } else {
            return "failure";
        }
    }
    @RequestMapping("doUserLogin")
    public String doUserLogin(User user, Model model,HttpSession HttpSession) {

        User queryUser =userService.login(user);
        if (queryUser !=null) {

            HttpSession.setAttribute("userInfo",queryUser);

            return "user-index";
        } else {
            model.addAttribute("message","账号或密码错误请重试");
            return "user-login";
        }
    }
    @RequestMapping("toUserIndex")
    public String toUserIndex(){

        return "user-index";
    }
    @RequestMapping("userLogout")
    public String toUserIndex(HttpSession HttpSession){
        HttpSession.invalidate();
        return "redirect:getAllTypeGoods";
    }

    @RequestMapping("getAllUserInfo")
    public String getAllUserInfo(Model model){

        List<User> list=userService.getAllUserInfo();

        model.addAttribute("userList",list);

        return "usersInfo";

    }
}
